package com.lp.utils;

import java.io.*;

public class docProcessing {
    //
    public static String readContentsFromTxt(String path,String encoding){
        try {
            File file = new File(path);
            if(file.isFile() && file.exists()){
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file),encoding);
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                StringBuffer contents = new StringBuffer();
                while((lineTxt = br.readLine()) != null){
                    contents.append(lineTxt);
                }
                br.close();
                isr.close();
                return contents.toString();
            }else{
                System.out.println("找不到指定的文件");
            }
        }catch (Exception e){
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return null;
    }
    //
    public static void writeContentsToTxt(String path,String contents){
        try {
            FileOutputStream fos = new FileOutputStream(path);
            fos.write(contents.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //
    public static String contentsFormatSwitch(String contents){
        String[] splits = contents.split(";");
        StringBuffer sb = new StringBuffer();
        for(String s:splits){
            sb.append(s + ";\r\n");
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        String contents = readContentsFromTxt("C:\\Users\\Administrator\\Desktop\\1.txt","UTF-8");
        System.out.println(contents);
        String s = contentsFormatSwitch(contents);
        System.out.println(s);
        writeContentsToTxt("C:\\Users\\Administrator\\Desktop\\2.txt",s);
    }
}


